package getsmart.project.activity;

import java.util.Random;

import getsmart.project.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

public class PlayMathematic2 extends ListActivity {
	int indexCursorRandomList = 0;
	int penunjuk = 1;
	int jumlahLoopforData = 1;
	int jumlahPertanyaan = 0;
	int tempRandomNumber = 0;
	String referQuest[];
	String jawabanRandom[];
	int randomPertanyaan[];
	int randomPosisiData[];
	int randomIndexPosisiData[];
	int randomForMultiDataDinamis[];
	String getJawabanAnak[];
	String tempData= "";
	ArrayAdapter<String> ad;
	String[][] listData;
	public int countAdapter = 0;
	TextView txtSoalMat;
	Button nextSoal;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.play_mathematic2);
		txtSoalMat = (TextView) findViewById(R.id.txtMatemathic2);
		nextSoal = (Button) findViewById(R.id.btnNextPlayMathematic2);
		referQuest = new String[4];
		referQuest[0] = "Ribuan";
		referQuest[1] = "Ratusan";
		referQuest[2] = "Puluhan";
		referQuest[3] = "Satuan";
		Random generator = new Random();
		jumlahPertanyaan = 5;
		getJawabanAnak = new String[jumlahPertanyaan];
		listData = new String[jumlahPertanyaan][4];
		randomForMultiDataDinamis = new int[4];
		randomPertanyaan = new int[jumlahPertanyaan];
		randomPosisiData = new int[jumlahPertanyaan];
		jawabanRandom = new String[jumlahPertanyaan];
		randomIndexPosisiData = new int[jumlahPertanyaan];
		for (int i = 0; i < jumlahPertanyaan; i++) {
			tempRandomNumber = generator.nextInt(9000);
			randomPertanyaan[i] = 1000 + tempRandomNumber;
		}
		for (int i = 0; i < jumlahPertanyaan; i++) {
			tempRandomNumber = generator.nextInt(4);
			randomPosisiData[i] = tempRandomNumber;
		}
		for (int i = 0; i < jumlahPertanyaan; i++) {
			tempData = tempData + randomPertanyaan[i];
			randomIndexPosisiData[i] = Integer
					.parseInt(tempData.substring(
							randomPosisiData[i], randomPosisiData[i]+1));
			tempData = "";
		}
		for (int i = 0; i < jumlahPertanyaan; i++) {
			if (randomPosisiData[i] == 0) {
				jawabanRandom[i] = "Ribuan";
			}
			if (randomPosisiData[i] == 1) {
				jawabanRandom[i] = "Ratusan";
			}
			if (randomPosisiData[i] == 2) {
				jawabanRandom[i] = "Puluhan";
			}
			if (randomPosisiData[i] == 3) {
				jawabanRandom[i] = "Satuan";
			}
		}
		for (int i = 0; i < jumlahPertanyaan; i++) {
			jumlahLoopforData = 1;
			while (jumlahLoopforData < 5) {
				if (jumlahLoopforData == 1) {
					tempRandomNumber = generator.nextInt(4);
					randomForMultiDataDinamis[0] = tempRandomNumber;
					listData[i][0] = referQuest[randomForMultiDataDinamis[0]];
					jumlahLoopforData++;
				} else {
					for (int in = 0; in < 3; in++) {
						while (cekDataContainInArray(jumlahLoopforData - 1,
								randomForMultiDataDinamis, tempRandomNumber) == 1) {
							tempRandomNumber = generator.nextInt(4);
						}
						randomForMultiDataDinamis[in + 1] = tempRandomNumber;
						listData[i][in + 1] = referQuest[randomForMultiDataDinamis[in + 1]];
						jumlahLoopforData++;
					}
				}
			}
		}
		penunjuk = randomPosisiData[0]+1;
		txtSoalMat.setText("Angka " + randomIndexPosisiData[0] +"pada posisi ke "+ penunjuk
				+ " pada bilangan " + randomPertanyaan[0] + "Bernilai :");
		ad = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice,
				android.R.id.text1, listData[countAdapter]);
		setListAdapter(ad);
		ListView listView = getListView();
		listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
		nextSoal.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (getJawabanAnak[jumlahPertanyaan - 1] != null) {
					Toast.makeText(
							getBaseContext(),
							"Nilai yang anda dapatkan adalah" + getResultFromTest(getJawabanAnak, jawabanRandom),
							Toast.LENGTH_LONG).show();
					Intent myIntent = new Intent(getApplicationContext(),
							ChooseMenu.class);
					myIntent.putExtra("ScoreUser",
							getResultFromTest(getJawabanAnak, jawabanRandom));
					startActivity(myIntent);
				}

				if (getJawabanAnak[countAdapter] == null) {
					Toast.makeText(
							getBaseContext(),
							"Anda belum memilih jawaban silahkan pilih jawaban",
							Toast.LENGTH_LONG).show();
				}

				else {
					countAdapter++;
					refreshAllData();
					if (countAdapter == jumlahPertanyaan - 1) {
						nextSoal.setText("Periksa");
					}
				}
			}
		});
	}

	public int cekDataContainInArray(int jumlahLoop, int[] arrDataCompare,
			int perbandinganData) {
		int defaultInt = 0;
		for (int i = 0; i < jumlahLoop; i++) {
			if (arrDataCompare[i] == perbandinganData) {
				defaultInt = 1;
				break;
			}
		}
		return defaultInt;
	}
	public void refreshAllData() {
		if (countAdapter != jumlahPertanyaan) {
			penunjuk = randomPosisiData[countAdapter]+1;
			txtSoalMat.setText("Angka " + randomIndexPosisiData[countAdapter] +"pada posisi ke "+ penunjuk
					+ " pada bilangan " + randomPertanyaan[countAdapter] + "Bernilai :");
			ad = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_single_choice,
					android.R.id.text1, listData[countAdapter]);
			setListAdapter(ad);

		}
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String item = (String) getListAdapter().getItem(position);
		insertJawabanAnak(item, countAdapter);
		if (countAdapter == jumlahPertanyaan - 1) {

		}

	}

	public void insertJawabanAnak(String data, int index) {
		getJawabanAnak[index] = data;
	}
	public int getResultFromTest(String[] jawabanAnak,String[] jawabanSistem){
		int re = 0;
		for (int i = 0; i < jawabanSistem.length; i++) {
			if (jawabanSistem[i].equalsIgnoreCase(jawabanAnak[i])) {
				re += 10;
			}
		}
		return re;
	}
}
